import {
  getDataList,
  getdataItems,
  getItemList
} from '../../api/documentModule'
const state = {
  showUploadBox: false,
  count: +'',
  itemCount: +'',
  tableData: [],
  showDeleteBox: false,
  // 上传弹框内得项目列表
  uploadItemList: [],
  // 是否展示编辑盒子
  showEditBox: false,
  itemList: [],
  // 是否展示添加类目盒子
  showAddItem: false,
  // 是否展示删除类目盒子
  showDeleteItemBox: false
}

const mutations = {
  // 是否展示更新盒子
  show_UploadBox (state, boolean) {
    state.showUploadBox = boolean
  },
  // 设置更新列表
  update_list (state, tableData) {
    state.tableData = tableData
  },
  // 总数
  list_count (state, count) {
    state.count = count
  },
  selectItem_list (state, uploadItemList) {
    state.uploadItemList = uploadItemList
  },
  show_editBox (state, boolean) {
    state.showEditBox = boolean
  },
  select_list (state, itemList) {
    state.itemList = itemList
  },
  show_addItemBox (state, boolean) {
    state.showAddItem = boolean
  },
  show_DeleteBox (state, boolean) {
    state.showDeleteBox = boolean
  },
  show_DeleteItemBox (state, boolean) {
    state.showDeleteItemBox = boolean
  },
  set_itemCount (state, count) {
    state.itemCount = count
  }
}

const actions = {
  // 刷新列表
  updateTunnelList (context, { pageSize, id, fileName, index, itemid }) {
    return new Promise((resolve, reject) => {
      getDataList(id, itemid, index, pageSize, fileName)
        .then(res => {
          if (res.data.code === (200 || '200')) {
            context.commit('update_list', res.data.data.data)
            context.commit('list_count', res.data.data.count)
          }
          resolve(res)
        })
        .catch(err => {
          reject(err)
        })
    })
  },
  // 获取类目选项
  async getdataItemsList (context, { index, item, pageSize }) {
    const res = await getdataItems(index, item, pageSize)
    context.commit('selectItem_list', res.data.data.data)
    context.commit('set_itemCount', res.data.data.count)
  },
  async getItemList (context, id) {
    const res = await getItemList(id)
    context.commit('select_list', res.data.data)
  }
}
export default {
  namespaced: true,
  actions,
  state,
  mutations
}
